package com.ruan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruan.entity.R;
import com.ruan.entity.SpecValue;
import com.ruan.mapper.SpecKeyMapper;
import com.ruan.service.SpecValueService;
import com.ruan.mapper.SpecValueMapper;
import com.ruan.utils.RedisCache;
import com.ruan.vo.SpecVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author 59499
* @description 针对表【spec_value】的数据库操作Service实现
* @createDate 2023-09-10 00:37:27
*/
@Service
public class SpecValueServiceImpl extends ServiceImpl<SpecValueMapper, SpecValue>
    implements SpecValueService{

    @Autowired
    private SpecValueMapper specValueMapper;

    @Autowired
    private SpecKeyMapper specKeyMapper;

    @Autowired
    private RedisCache redisCache;

    @Override
    public R<List<SpecVo>> getSpecOptionsBySpuId(Integer spuId) {
        QueryWrapper<SpecValue> wrapper = new QueryWrapper<>();
        wrapper.eq("spu_id",spuId);
        List<SpecValue> specValueList = specValueMapper.selectList(wrapper);
        List<SpecVo> specVoList1 = new ArrayList<>();
        for (SpecValue specValue:specValueList){
            String[] options = specValue.getValue().split(",");
            String title = specKeyMapper.selectById(specValue.getSpecKeyId()).getTitle();
            Integer id = specValue.getSpecKeyId();
            SpecVo specVo = new SpecVo(id,title,options);
            specVoList1.add(specVo);
        }
        return new R<>(200,"查询成功",specVoList1);
    }
}




